//
//  ProfileOrderCell.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/25.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

extension UIImage {
    static let redRoundedImage = UIImage(by: .themeRedColor, size: CGSize(width: 30, height: 30), cornerRadius: 8, borderWidth: 0, borderColor: nil, stretch: true)!
    static let whiteRoundedImage = UIImage(by: .white, size: CGSize(width: 30, height: 30), cornerRadius: 8, borderWidth: 0, borderColor: nil, stretch: true)!
}

extension UIColor {
    /// 浅灰背景色 #f4f4f4 RGB 244, 244, 244
    static let themebackgroundColor = UIColor(hexString: "#f4f4f4")!

    /// 红色 #ff4259
    static let themeRedColor = UIColor(hexString: "#ff4259")!

    /// 中灰色 #666666
    static let themeGrayColor = UIColor(hexString: "#666666")!

    /// 浅灰色 #9B9B9B
    static let themeLightGrayColor = UIColor(hexString: "#9B9B9B")!

    /// 深灰色 #363636 RGB 54, 54, 54
    static let themeDarkGrayColor = UIColor(hexString: "#363636")!

    /// 分割线颜色 #979797
    static let themeLineColor = UIColor(hexString: "#979797")!
}

class ProfileOrderCell: UITableViewCell {

    var buttons: [OrderButton] = []
    let images = [#imageLiteral(resourceName: "制版"), #imageLiteral(resourceName: "生产 copy"), #imageLiteral(resourceName: "备货 copy"), #imageLiteral(resourceName: "已发货 copy 2"), #imageLiteral(resourceName: "签收 copy 2")]

    let leftLineView = LeftLineView()
    let searchView = SearchItemsView()

    private var serviceItemViews: [ServiceItemView] = []

    private let ser1 = Service(title: "在线客服", marks: "适用人群\n喜欢在线交流的用户", actionTitle: "立即咨询", inverse: false)
    private let ser2 = Service(title: "热线电话", marks: "客户服务热线\n服务时间：7:00 - 21:30", actionTitle: "4008-919-857", inverse: true)
    private let ser3 = Service(title: "提建议", marks: "如果您对我司产品有任何建议，欢迎提出", actionTitle: "提建议", inverse: false)
    private lazy var services: [Service] = [self.ser1, self.ser2, self.ser3]

    //订单
    let orderView: UIView = {
        let view = UIView()

        return view
    }()

    //客服
    let serviceView: UIView = {
        let view = UIView()

        return view
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        searchView.rightButton.setImage(#imageLiteral(resourceName: "profile_btn_order").alwaysOriginal(), for: .normal)
        leftLineView.titleLabel.text = "我的订单"

        contentView.addSubview(orderView)
        serviceView.addLine(on: .left, color: .gray, lineWidth: 1 / UIScreen.main.scale, inset: UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0))
        contentView.addSubview(serviceView)
        orderView.addSubview(leftLineView)
        orderView.addSubview(searchView)

        let texts = ["制版", "生产", "备货", "已发货", "已签收"]

        //左侧订单按钮
        for (index, image) in images.enumerated() {
            let button = OrderButton(type: .system)
            button.setImage(image.alwaysOriginal(), for: .normal)
            button.textLabel.text = texts[index]
            button.numberLabel.text = "(\(234))"
            button.tag = index
            orderView.addSubview(button)
            buttons.append(button)
        }

        for service in services {
            let view = ServiceItemView()
            view.service = service
            serviceView.addSubview(view)
            serviceItemViews.append(view)
        }

        orderView.mas_makeConstraints { (make) in
            make?.top.left().bottom().equalTo()(contentView)
            make?.width.equalTo()(contentView)?.multipliedBy()(0.5)
        }

        serviceView.mas_makeConstraints { (make) in
            make?.top.bottom().equalTo()(contentView)
            make?.right.equalTo()(contentView)?.offset()(30)
            make?.width.equalTo()(contentView)?.multipliedBy()(0.5)
        }

        leftLineView.mas_makeConstraints { (make) in
            make?.left.equalTo()(orderView)?.offset()(30)
            make?.top.equalTo()(orderView)?.offset()(25)
        }

        searchView.mas_makeConstraints { (make) in
            make?.right.equalTo()(orderView)
            make?.centerY.equalTo()(leftLineView)
            make?.size.equalTo()(CGSize(width: 350, height: 30))
        }

        addLine(on: .bottom, color: .gray, lineWidth: 1 / UIScreen.main.scale, inset: UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        orderView.distributeSquareSubviews(buttons, centerYOffset: 0, type: .autoLayout)

        serviceView.distributeSubviews(serviceItemViews, insets: UIEdgeInsets(top: 25, left: 30, bottom: 30, right: 30), itemSpacing: 15, type: .autoLayout)
    }

    class OrderButton: UIButton {

        let textLabel: UILabel = {
            let label = UILabel()
            label.textAlignment = .left
            label.numberOfLines = 1
            label.font = UIFont.systemMediumFont(withSize: 14)
            label.textColor = .black;
            return label
        }()

        let numberLabel: UILabel = {
            let label = UILabel()
            label.textAlignment = .left
            label.numberOfLines = 1
            label.font = UIFont.systemRegularFont(withSize: 10)
            label.textColor = .themeRedColor
            return label
        }()

        override init(frame: CGRect) {
            super.init(frame: frame)

            setup()
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)

            setup()
        }

        private func setup() {
            addSubview(textLabel)
            addSubview(numberLabel)
            textLabel.mas_makeConstraints { (make) in
                make?.centerX.equalTo()(imageView!)
                make?.top.equalTo()(imageView!.mas_bottom)
            }
            numberLabel.mas_makeConstraints { (make) in
                make?.centerX.equalTo()(textLabel)
                make?.top.equalTo()(textLabel.mas_bottom)
            }
        }
    }
}

fileprivate extension ProfileOrderCell{

    struct Service {
        let title: String
        let marks: String
        let actionTitle: String
        let inverse: Bool
    }

    class ServiceItemView: UIView {

        var service: Service? {
            didSet {
                leftLineView.titleLabel.text = service?.title
                button.setTitle(service?.actionTitle, for: .normal)
                label.text = service?.marks
                if service?.inverse ?? false {
                    button.setTitleColor(.themeRedColor, for: .normal)
                    button.setBackgroundImage(nil, for: .normal)
                } else {
                    button.setTitleColor(.white, for: .normal)
                    button.setBackgroundImage(.redRoundedImage, for: .normal)
                }
            }
        }

        let leftLineView = LeftLineView()

        let label: UILabel = {
            let label = UILabel()
            label.textAlignment = .left
            label.numberOfLines = 2
            label.font = UIFont.systemFont(ofSize: 12)
            label.textColor = UIColor(hexString: "#666666");
            return label
        }()

        lazy var button: UIButton = {
            let button = UIButton(type: .system)
            button.titleLabel?.font = .systemMediumFont(withSize: 12)
            return button
        }()

        override init(frame: CGRect) {
            super.init(frame: frame)

            setup()
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)

            setup()
        }

        private func setup() {
            addSubview(leftLineView)
            addSubview(button)
            addSubview(label)

            leftLineView.mas_makeConstraints { (make) in
                make?.top.equalTo()(self)
                make?.left.right().equalTo()(self)
                make?.height.equalTo()(30)
            }

            label.mas_makeConstraints { (make) in
                make?.top.equalTo()(leftLineView.mas_bottom)?.offset()(25)
                make?.left.right().equalTo()(self)
            }

            button.mas_makeConstraints { (make) in
                make?.centerX.equalTo()(self)
                make?.height.equalTo()(30)
                make?.width.equalTo()(100)
                make?.bottom.equalTo()(self)
            }   
        }
    }
}




